package com.x.compute.example;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.x.compute.core.context.VariableContext;
import com.x.compute.core.entity.Compute;
import com.x.compute.core.entity.ComputeExpression;
import com.x.compute.core.entity.ComputeParam;
import com.x.compute.core.service.ComputeService;
import com.x.compute.core.util.ComputeParamUtils;
import com.x.compute.entity.Product;
import com.x.compute.entity.ProductParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 业务示例
 * @author xuxin
 */
@RestController
@RequestMapping("/example/business")
public class BusinessExample {

    @Resource
    private ComputeService computeService;

    /**
     * 新增变量计算示例
     *
     * @return
     */
    @PostMapping("/add")
    public void add() {
        ProductParam param = getParam();
        Long formId = 1001L;
        Long id = IdUtil.getSnowflakeNextId();
        Product product = new Product();
        product.setId(id);
        product.setFormId(formId);
        product.setName(param.getName());

        //第一种表格中变量少的情况，手动生成计算变量
        //name变量来源固定值
        ComputeParam nameParam = ComputeParamUtils.buildWithoutVar(id.toString(),Product.class,"name",product.getName());
        //quantity、unitPrice变量来源公式计算
        ComputeParam quantityParam = ComputeParamUtils.buildNumberVar(id.toString(),Product.class,"quantity",param.getQuantity());
        product.setQuantity(quantityParam.getId());
        ComputeParam unitPriceParam = ComputeParamUtils.buildNumberVar(id.toString(), Product.class,"unitPrice",param.getUnitPrice());
        product.setUnitPrice(quantityParam.getId());
        computeService.addCompute(formId, nameParam, quantityParam, unitPriceParam);
        //第二种表格中变量多的情况，自动生成计算变量并注入关联计算id
//        List<ComputeParam> paramList = ComputeParamUtils.buildVars(id.toString(), param, product);
//        computeService.addCompute(formId, paramList);

        save(product);
    }

    /**
     * 查询变量计算示例
     *
     * @return
     */
    @GetMapping("/get")
    public void get() {
        Long formId = 1001L;
        Long businessId = 1731508677905334272L;
        Map<String, String> valueMap = computeService.getVarValue(formId, businessId.toString());
        System.out.println(JSONUtil.toJsonStr(valueMap));

        Map<String, Compute> computeMap = computeService.getCompute(formId, businessId.toString());
        System.out.println(JSONUtil.toJsonStr(computeMap));
    }

    /**
     * 修改变量计算示例
     *
     * @return
     */
    @PostMapping("/update")
    public void update() {
        ProductParam param = getParam();
        Long formId = 1001L;
        Long id = 1731508880066592769L;
        Product product = new Product();
        product.setId(id);
        product.setFormId(formId);
        product.setName(param.getName());

        //第一种表格中变量少的情况，手动生成计算变量
        //name变量来源固定值
        ComputeParam nameParam = ComputeParamUtils.buildWithoutVar(id.toString(),Product.class,"name",product.getName());
        //quantity、unitPrice变量来源公式计算
        ComputeParam quantityParam = ComputeParamUtils.buildNumberVar(id.toString(),Product.class,"quantity",param.getQuantity());
        product.setQuantity(quantityParam.getId());
        ComputeParam unitPriceParam = ComputeParamUtils.buildNumberVar(id.toString(), Product.class,"unitPrice",param.getUnitPrice());
        product.setUnitPrice(quantityParam.getId());
        computeService.updateCompute(formId, nameParam, quantityParam, unitPriceParam);
        //第二种表格中变量多的情况，自动生成计算变量并注入关联计算id
//        List<ComputeParam> paramList = ComputeParamUtils.buildVars(id.toString(), param, product);
//        computeService.updateCompute(formId, paramList);

        save(product);
    }

    /**
     * 删除变量计算示例
     *
     * @return
     */
    @PostMapping("/delete")
    public void delete() {
        Long formId = 1001L;
        Long businessId = 1731508880066592769L;
        String tableId = VariableContext.getTableId(Product.class);
        computeService.deleteCompute(formId, tableId, businessId);

        deleteById(businessId);
    }


    private static ProductParam getParam(){
        ProductParam param = new ProductParam();
        param.setId(IdUtil.getSnowflakeNextId());
        param.setName("手机");
        param.setQuantity(new ComputeExpression("1","数量"));
        param.setUnitPrice(new ComputeExpression("1000*3","单价"));
        return param;
    }

    /**
     * 模拟入库
     * @param obj
     */
    private void save(Object obj){
    }

    /**
     * 模拟删除
     * @param businessId
     */
    private void deleteById(Long businessId) {
    }
}